﻿using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class K9316
    {
        public K9316()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            InitializeComponent();
            SDSA = "9316";
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded) return;
            base.Window_Loaded(sender, e);
            // LOADALL();
            NormalTask = Task.Factory.StartNew(() => LOADALL(), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
           if(dwMain==null)
            {

            }
            dwKEY = dwMaster;
            try
            {
                if (dwKEY != null)
                {
                    KEY = dwKEY["KEY"].ToString();
                    DES = Helpers.Helper.ConvertGB2312To8859P1(dwKEY["DES"].ToString());
                    SDES = Helpers.Helper.ConvertGB2312To8859P1(dwKEY["SDES"].ToString());
                    if (TBMain.Columns.Contains("VALUE"))
                    {
                        VALUE = dwKEY["VALUE"].ToString();
                    }
                }
                else
                {

                    KEY = "0";
                    DES = "";
                    SDES = "";
                    VALUE = "000000";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void LOADALL()
        {
            base.LOADALL();
            foreach (DataRow row in TBMain.Rows)
            {
                row["SDES"] = Helpers.Helper.Convert8859P1ToGB2312(row["SDES"].ToString().Trim());
                row["DES"] = Helpers.Helper.Convert8859P1ToGB2312(row["DES"].ToString().Trim());
            }
            if (SDSA.Length == 0) return;
            if (TBMain.Rows.Count > 0)
                return;
            if (DBSDDAT.ContainsKey(SDSA))
            {
                DBSDDAT.Remove(SDSA);
            }
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData, SDSA);
            dwData["KEY"]=Helpers.Helper.ConvertGB2312To8859P1("1");
            dwData["DES"]=Helpers.Helper.ConvertGB2312To8859P1("100");
            dwData["SDES"]=Helpers.Helper.ConvertGB2312To8859P1("100");
            dwData["VALUE"]=Helpers.Helper.ConvertGB2312To8859P1("000000000"); 
            TMV.Save(dwData);
            dwData["KEY"]=Helpers.Helper.ConvertGB2312To8859P1("2");
            dwData["DES"]=Helpers.Helper.ConvertGB2312To8859P1("200");
            dwData["SDES"]=Helpers.Helper.ConvertGB2312To8859P1("200");
            dwData["VALUE"]=Helpers.Helper.ConvertGB2312To8859P1("000000000"); 
            TMV.Save(dwData);
            dwData["KEY"] = Helpers.Helper.ConvertGB2312To8859P1("3");
            dwData["DES"] = Helpers.Helper.ConvertGB2312To8859P1("300");
            dwData["SDES"] = Helpers.Helper.ConvertGB2312To8859P1("300");
            dwData["VALUE"] = Helpers.Helper.ConvertGB2312To8859P1("000000000");
            TMV.Save(dwData);
            dwData["KEY"] = Helpers.Helper.ConvertGB2312To8859P1("4");
            dwData["DES"] = Helpers.Helper.ConvertGB2312To8859P1("400");
            dwData["SDES"] = Helpers.Helper.ConvertGB2312To8859P1("400");
            dwData["VALUE"] = Helpers.Helper.ConvertGB2312To8859P1("000000000");
            TMV.Save(dwData);
            dwData["KEY"] = Helpers.Helper.ConvertGB2312To8859P1("5");
            dwData["DES"] = Helpers.Helper.ConvertGB2312To8859P1("500");
            dwData["SDES"] = Helpers.Helper.ConvertGB2312To8859P1("500");
            dwData["VALUE"] = Helpers.Helper.ConvertGB2312To8859P1("000000000");
            TMV.Save(dwData);
            LOADALL();
        }
    }
}
